home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / SBITS.H < prev    next >
C/C++ Source or Header  |  1993-05-19  |  4KB  |  106 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* sbits.h */
  20. /* Definitions for Ghostscript bit-oriented streams */
  21. /* Requires stream.h */
  22.  
  23. /*
  24.  * Several filters read or write non-byte-aligned data
  25.  * (primarily, but not exclusively, Huffman codes).
  26.  * The macros and procedures declared in this file support this.
  27.  * Note that they distinguish `s', the bit-oriented filter stream,
  28.  * from `strm', the underlying stream supplying or receiving bytes.
  29.  */
  30.  
  31. /* Define the size of the buffer for reading bits. */
  32. #define sbits_size (arch_sizeof_int == 2 ? 16 : 32)
  33.  
  34. /* ------ Reading bits ------ */
  35.  
  36. /*
  37.  * Invariants when reading bits:
  38.  *    0 <= bits_left <= bits_size;
  39.  *    bits[bits_left-1..0] contain valid (unread) data.
  40.  */
  41.  
  42. /* Declare a table for reversing the bit order of each byte. */
  43. extern const byte sbits_reverse_bits[256];
  44.  
  45. /* Initialize for reading bits. */
  46. #define sgetbits_init(s, reverse)\
  47.   ((s)->bits = 0, (s)->bits_left = 0, (s)->reverse_bits = reverse)
  48.  
  49. /* Declare variables for reading bits. */
  50. /* Note that the following names are used, always: */
  51. /*    strm, bits, bits_left, bits_cp, bits_ep    */
  52. #define sbits_declare_inline\
  53.   stream *strm = s->strm;\
  54.   s_declare_inline(strm, bits_cp, bits_ep);\
  55.   uint bits;\
  56.   int bits_left
  57. /* Begin and end inline bit reading. */
  58. #define sbits_begin_inline(s)\
  59.   s_begin_inline(strm, bits_cp, bits_ep),\
  60.   bits = (s)->bits, bits_left = (s)->bits_left
  61. #define sbits_end_inline(s)\
  62.   s_end_inline(strm, bits_cp, bits_ep),\
  63.   (s)->bits = bits, (s)->bits_left = bits_left
  64.  
  65. /* Ensure at least n valid bits in the buffer. */
  66. /* n must not be greater than 9. */
  67. #define sbits_ensure_inline(n)\
  68.   if ( bits_left < n ) sbits_more()
  69. #define sbits_more_1()\
  70.   { int c = sgetc_inline(strm, bits_cp, bits_ep);\
  71.     if ( c < 0 ) { s->end_status = c; goto out; }\
  72.     if ( s->reverse_bits ) c = sbits_reverse_bits[c];\
  73.     bits = (bits << 8) + c, bits_left += 8;\
  74.   }
  75. #if sbits_size == 16
  76. #  define sbits_more() sbits_more_1()
  77. #else                /* sbits_size >= 32 */
  78. #  define sbits_more()\
  79.   { if ( bits_ep - bits_cp < 3 ) sbits_more_1()\
  80.     else\
  81.     { if ( s->reverse_bits )\
  82.     bits = (bits << 24) + ((uint)sbits_reverse_bits[bits_cp[0]] << 16) + ((uint)sbits_reverse_bits[bits_cp[1]] << 8) + sbits_reverse_bits[bits_cp[2]];\
  83.       else\
  84.     bits = (bits << 24) + ((uint)bits_cp[0] << 16) + ((uint)bits_cp[1] << 8) + bits_cp[2];\
  85.       bits_left += 24, bits_cp += 3;\
  86.     }\
  87.   }
  88. #endif
  89.  
  90. /* Peek at the next n bits (known to be available). */
  91. /* Use peek_bits if n is constant, peek_var_bits if n is variable. */
  92. extern const byte sbits_peek_masks[9];
  93. #define sbits_peek_inline(n)\
  94.   ((bits >> (bits_left - (n))) & ((1 << (n)) - 1))
  95. #define sbits_peek_var_inline(n)\
  96.   ((bits >> (bits_left - (n))) & sbits_peek_masks[n])
  97.  
  98. /* Skip over n bits (known to be available). */
  99. #define sbits_skip_inline(n) bits_left -= (n)
  100.  
  101. /* ------ Writing bits ------ */
  102.  
  103. /* Initialize for reading or writing bits. */
  104. #define sputbits_init(s, reverse)\
  105.   ((s)->bits = 0, (s)->bits_left = sbits_size, (s)->reverse_bits = reverse)
  106.